package com.example.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Component;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

@Aspect
@Component
public class SysLogAspect {
    /**
     * 切面
     */
    @Pointcut("@annotation(com.example.aop.SysLog)")
    public void logPointCut() {

    }

    @Before("logPointCut() && @annotation(sysLog)")
    public void doBefore(JoinPoint joinPoint, SysLog sysLog) {
        System.out.println("==========执行controller前置通知===============");
    }

    /**
     * 环绕通知
     *
     * @param point
     * @return
     * @throws Throwable
     */
    @Around("logPointCut()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        long beginTime = System.currentTimeMillis();
        //执行方法
        Object result = point.proceed();
        //执行时长(毫秒)
        long time = System.currentTimeMillis() - beginTime;

        // 获取方法
        MethodSignature signature = (MethodSignature) point.getSignature();
        Method method = signature.getMethod();
        Object[] args = point.getArgs();

        return result;
    }

    @After("logPointCut() && @annotation(abc)")
    public void doAfter(JoinPoint joinPoint, SysLog abc) {
        System.out.println("==========执行controller前置通知===============");
    }

    public void pointMethod(JoinPoint joinPoint) throws ClassNotFoundException {
        joinPoint.getArgs();//輸入的參數列表

        joinPoint.getTarget().getClass().getName();//類全路徑

        joinPoint.getSignature().getDeclaringTypeName();//接口全路徑

        joinPoint.getSignature().getName();//調用的方法
    }
}
